package io.qytc.jboot.support.auth;

import com.jfinal.aop.Aop;
import io.jboot.components.rpc.JbootrpcServiceConfig;
import io.jboot.components.rpc.annotation.RPCInject;
import io.qytc.jboot.base.common.ServiceConst;
import io.qytc.jboot.base.plugin.shiro.auth.MuitiAuthenticatied;
import io.jboot.Jboot;
import io.qytc.jboot.service.api.ResService;
import io.qytc.jboot.service.api.RoleService;
import io.qytc.jboot.service.api.UserService;
import io.qytc.jboot.service.entity.model.Res;
import io.qytc.jboot.service.entity.model.Role;
import io.qytc.jboot.service.entity.model.User;
import io.qytc.jboot.service.entity.status.system.UserStatus;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理端认证授权
 * @author Rlax
 *
 */
public class LoginAuth implements MuitiAuthenticatied {

    UserService userService;

    RoleService roleService;

    ResService resService;

    public LoginAuth(){
        if (userService==null){
            JbootrpcServiceConfig config=new JbootrpcServiceConfig();
            config.setGroup(ServiceConst.SERVICE_SYSTEM);
            config.setVersion(ServiceConst.VERSION_1_0);
            userService=Jboot.service(UserService.class,config);
        }
        if (roleService==null){
            JbootrpcServiceConfig config=new JbootrpcServiceConfig();
            config.setGroup(ServiceConst.SERVICE_SYSTEM);
            config.setVersion(ServiceConst.VERSION_1_0);
            roleService=Jboot.service(RoleService.class,config);
        }
        if (resService==null){
            JbootrpcServiceConfig config=new JbootrpcServiceConfig();
            config.setGroup(ServiceConst.SERVICE_SYSTEM);
            config.setVersion(ServiceConst.VERSION_1_0);
            resService=Jboot.service(ResService.class,config);
        }

    }



    @Override
    public boolean hasToken(AuthenticationToken authenticationToken) {
        String loginName = authenticationToken.getPrincipal().toString();
        return userService.hasUser(loginName);
    }

    @Override
    public boolean wasLocked(AuthenticationToken authenticationToken) {
        String loginName = authenticationToken.getPrincipal().toString();
        User sysUser = userService.findByName(loginName);
        return !sysUser.getStatus().equals(UserStatus.USED);
    }

    @Override
    public AuthenticationInfo buildAuthenticationInfo(AuthenticationToken authenticationToken) {
        String loginName = authenticationToken.getPrincipal().toString();

        User sysUser = userService.findByName(loginName);
        String salt2 = sysUser.getSalt2();
        String pwd = sysUser.getPwd();

        return new SimpleAuthenticationInfo(loginName, pwd, ByteSource.Util.bytes(salt2), "ShiroDbRealm");
    }

    @Override
    public AuthorizationInfo buildAuthorizationInfo(PrincipalCollection principals) {
        String loginName = (String) principals.fromRealm("ShiroDbRealm").iterator().next();

        List<Role> sysRoleList = roleService.findByUserName(loginName);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        List<String> roleNameList = new ArrayList<String>();
        for (Role sysRole : sysRoleList) {
            roleNameList.add(sysRole.getName());
        }
        List<Res> sysResList = resService.findByUserNameAndStatusUsed(loginName);
        List<String> urls = new ArrayList<String>();
        for (Res sysRes : sysResList) {
            urls.add(sysRes.getUrl());
        }

        info.addRoles(roleNameList);
        info.addStringPermissions(urls);
        return info;
    }
}
